package cn.stylefeng.guns.sys.modular.tao.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.tao.entity.Goods;
import cn.stylefeng.guns.sys.modular.tao.entity.PictureVideo;
import cn.stylefeng.guns.sys.modular.tao.model.params.PictureVideoParam;
import cn.stylefeng.guns.sys.modular.tao.service.GoodsService;
import cn.stylefeng.guns.sys.modular.tao.service.PictureVideoService;
import cn.stylefeng.guns.sys.modular.tao.shiUtil.ResponseMsg;
import cn.stylefeng.guns.sys.modular.tao.shiUtil.UploadFileUtils;
import cn.stylefeng.guns.sys.modular.tao.shiUtil.UploadResult;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.FileNotFoundException;
import java.util.Date;


/**
 * 控制器
 *  视频图文表
 * @author 石博士
 * @Date 2019-11-06 14:44:30
 */
@Controller
@RequestMapping("/pictureVideo")
public class PictureVideoController extends BaseController {

    private static final String SystemConstant = "https://hybn-1259399233.cos.ap-chengdu.myqcloud.com/";


    /**
     * 图文 URL
     */
    public String pictureURL = "";

    /**
     * 视频 URL
     */
    public String videoURL = "";





    private String PREFIX = "/pictureVideo";


    /**
     * 图文视频表
     */
    @Autowired
    private PictureVideoService pictureVideoService;


    /**
     * 商品表
     */
    @Autowired
    private GoodsService goodsService;




    /**
     * 跳转到主页面
     *
     * @author 石博士
     * @Date 2019-11-06
     */
    @RequestMapping("")
    public String index() {
        pictureURL = "";
        videoURL = "";
        return PREFIX + "/pictureVideo.html";
    }


    /**
     * 新增页面
     *
     * @author 石博士
     * @Date 2019-11-06
     */
    @RequestMapping("/add")
    public String add() {
        pictureURL = "";
        videoURL = "";
        return PREFIX + "/pictureVideo_add.html";
    }


    /**
     * 编辑页面
     *
     * @author 石博士
     * @Date 2019-11-06
     */
    @RequestMapping("/edit")
    public String edit() {
        pictureURL = "";
        videoURL = "";
        return PREFIX + "/pictureVideo_edit.html";
    }


    /**
     * 新增接口
     *
     * @author 石博士
     * @Date 2019-11-06
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(PictureVideoParam pictureVideoParam) {
        pictureURL = "";
        videoURL = "";
        this.pictureVideoService.add(pictureVideoParam);
        return ResponseData.success();
    }





    /**
     * 修改数据接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(HttpSession session , PictureVideoParam pictureVideoParam) {
        pictureURL = "";
        videoURL = "";
        /**
         * 获取商品的 ID 查询商品详情
         */
        Goods goods = goodsService.getByIds(Long.parseLong(session.getAttribute("goodsId").toString()));
        if(ToolUtil.isEmpty(goods.getPictureVideoId())){
            pictureVideoService.add(pictureVideoParam);
            goods.setId(goods.getId());
            goods.setPictureVideoId(pictureVideoService.pictureVideoMax());
            goodsService.updataPictureVideoId(goods);
        }else{
            pictureVideoService.update(pictureVideoParam);
        }


        return ResponseData.success();
    }





    /**
     * 删除接口
     *
     * @author 石博士
     * @Date 2019-11-06
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(PictureVideoParam pictureVideoParam) {
        pictureURL = "";
        videoURL = "";
        this.pictureVideoService.delete(pictureVideoParam);
        return ResponseData.success();
    }


    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(PictureVideoParam pictureVideoParam,HttpSession session) {
        pictureURL = "";
        videoURL = "";
        Goods goods = goodsService.getByIds(Long.parseLong(session.getAttribute("goodsId").toString()));
        if(ToolUtil.isEmpty(goods.getPictureVideoId())){
            pictureVideoParam.setUploadTime(new Date());
            pictureVideoService.add(pictureVideoParam);
            goods.setId(goods.getId());
            goods.setPictureVideoId(pictureVideoService.pictureVideoMax());
            goodsService.updataPictureVideoId(goods);
        }
        PictureVideo detail = this.pictureVideoService.getById(goods.getPictureVideoId());
        session.setAttribute("pictureVideoId",detail.getId());
        return ResponseData.success(detail);
    }


    /**
     * 查询列表
     *
     * @author 石博士
     * @Date 2019-11-06
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(PictureVideoParam pictureVideoParam) {
        pictureURL = "";
        videoURL = "";
        return this.pictureVideoService.findPageBySpec(pictureVideoParam);
    }






    /**
     * 上传图文
     */
    @RequestMapping(method = RequestMethod.POST, path = "/picture")
    @ResponseBody
    public ResponseData picture(@RequestPart("file") MultipartFile file, HttpSession session) throws FileNotFoundException {

        if (ToolUtil.isEmpty(file)){
            return  ResponseData.error(201,"上传失败，图片为空");
        }else{
            ResponseMsg msg = UploadFileUtils.uploadFile(file);
            if (msg.getStatus() == 10001){
                UploadResult result = (UploadResult) msg.getResult();
                pictureURL+=SystemConstant+result.getPath()+",";
                return  ResponseData.success(200,"上传成功",pictureURL);
            }else {
                return  ResponseData.error(202,"上传失败，图片格式错误");
            }
        }
    }


    /**
     * 上传视频
     */
    @RequestMapping(method = RequestMethod.POST, path = "/video")
    @ResponseBody
    public ResponseData video(@RequestPart("file") MultipartFile file, HttpSession session) throws FileNotFoundException {

        if (ToolUtil.isEmpty(file)){
            return  ResponseData.error(201,"上传失败，视频为空");
        }else{
            ResponseMsg msg = UploadFileUtils.uploadFile(file);
            if (msg.getStatus() == 10001){
                UploadResult result = (UploadResult) msg.getResult();
                videoURL+=SystemConstant+result.getPath()+",";
                return  ResponseData.success(200,"上传成功",videoURL);
            }else {
                return  ResponseData.error(202,"上传失败，视频格式错误");
            }
        }
    }



    /**
     * 删除图片接口
     */
    @RequestMapping(method = RequestMethod.POST, path = "/updateImg")
    @ResponseBody
    public ResponseData updateImg(PictureVideo pictureVideo, HttpSession session, @RequestParam(value = "img", required = false) String img) {
        pictureVideo.setId(Integer.parseInt(session.getAttribute("pictureVideoId").toString()));
        pictureVideo.setPictureUrl(img);
        pictureVideoService.updateImg(pictureVideo);
        return ResponseData.success("删除成功");
    }








}


